package com.sofoscorp.android.gestcobranzamobile;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class KNB1_BO {
	private String C_TABNAME = "KNB1";
	private ConexionDB go_conexDB;
	private Cursor gc_KNB1;
	private SQLiteDatabase go_db;
	
	private KNB1_Str gs_KNB1 = null;
	private KNB1_Str[] gt_KNB1 = null;
	String[] gt_campos = new String[]{"BUKRS", "KUNNR", "ZTERM", "TEXTO"};
	
	public KNB1_BO(){
		go_conexDB = new ConexionDB();
	}
	
	public KNB1_Str[] get_SingleByBUKRS(String ivBUKRS){
		ArrayList<KNB1_Str> lt_List = new ArrayList<KNB1_Str>();
		String lv_Where = "BUKRS = ?";
		String[] lt_WhereArgs;
		
		if(ivBUKRS.equals("") || ivBUKRS.equals(null)){
			gs_KNB1 = null;
		}else {
			gc_KNB1 = null;
			lt_WhereArgs = new String[]{ivBUKRS};
			
			//Pasar el Path de DB
			go_conexDB.serialization(go_conexDB.C_PATH_FROMSAP);

			//Verificar si existe conexion con la DB
			if (go_conexDB.checkConexDB()) {

				//Obtener la DB
				go_db = go_conexDB.getGo_DB();
				
				try {
					//Ejecutar Select a la DB
					gc_KNB1 = go_db.query(C_TABNAME, gt_campos, lv_Where, lt_WhereArgs, null, null, null, null);
					if (gc_KNB1.moveToFirst()) {
						//Recorrer el cursor
						do {
							lt_List.add(new KNB1_Str(	gc_KNB1.getString(0), 
														gc_KNB1.getString(1), 
														gc_KNB1.getString(2), 
														gc_KNB1.getString(3)));
						} while (gc_KNB1.moveToNext());
						gt_KNB1 = lt_List.toArray(new KNB1_Str[0]);
					}else{
						lt_List = null;
						gt_KNB1 = null;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}else{
				lt_List = null;
				gt_KNB1 = null;
			}
		}

		go_db.close();
		gc_KNB1.close();
		return gt_KNB1;
	}
	
	public KNB1_Str get_SingleByKey(String ivBUKRS, String ivKUNNR){
		String lv_Where = "BUKRS = ? AND KUNNR = ?";
		String[] lt_WhereArgs;
		
		if(ivBUKRS.equals("") || ivBUKRS.equals(null)){
			gs_KNB1 = null;
		}else {
			gc_KNB1 = null;
			lt_WhereArgs = new String[]{ivBUKRS, ivKUNNR};
			
			//Pasar el Path de DB
			go_conexDB.serialization(go_conexDB.C_PATH_FROMSAP);

			//Verificar si existe conexion con la DB
			if (go_conexDB.checkConexDB()) {

				//Obtener la DB
				go_db = go_conexDB.getGo_DB();
				
				try {
					//Ejecutar Select a la DB
					gc_KNB1 = go_db.query(C_TABNAME, gt_campos, lv_Where, lt_WhereArgs, null, null, null, null);
					if (gc_KNB1.moveToFirst()) {
						gs_KNB1 = new KNB1_Str(	gc_KNB1.getString(0), 
												gc_KNB1.getString(1), 
												gc_KNB1.getString(2), 
												gc_KNB1.getString(3));
					}else{
						gs_KNB1 = null;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}else{
				gs_KNB1 = null;
			}
		}

		go_db.close();
		gc_KNB1.close();
		return gs_KNB1;
	}
}
